# coding=utf-8
"""
功能：
1.发送邮件
    配置邮箱服务器、端口、TLS加密、登录用户名与应用授权码
    配置邮件发件人、收件人、标题与邮件内容
"""
from flask import Flask
from flask_mail import Mail, Message
import os
import ConfigParser

user_pswd_cfg = os.environ['user_pswd']
cp = ConfigParser.ConfigParser()
cp.read(user_pswd_cfg)
user = cp.get('qq_email', 'qq_email_user')
pswd = cp.get('qq_email', 'qq_email_pswd')
app = Flask(__name__)
app.config.update(
    DEBUG=True,
    MAIL_SERVER='smtp.qq.com',
    MAIL_PROT=465,
    MAIL_USE_TLS=True,
    MAIL_USERNAME=user,
    MAIL_PASSWORD=pswd,
)
mail = Mail(app)


@app.route('/')
def index():
    msg = Message(subject="Hello flask_mail", html='Hello flask_mail', sender=app.config.get('MAIL_USERNAME'),
                  recipients=[app.config.get('MAIL_USERNAME'), ])
    mail.send(msg)
    print "Mail sent"
    return "Send successfully"


def _test_index():
    """
    _test_index()
    >
    192.168.99.80:5000
    """
    app.run(host='0.0.0.0', debug=True)


if __name__ == '__main__':
    _test_index()
